package com.ruoyi.car.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.car.domain.CarGoods;
import com.ruoyi.car.domain.CarOrder;
import com.ruoyi.car.domain.vo.CarTypeOrderCountVo;
import com.ruoyi.car.domain.vo.NameValueVo;
import com.ruoyi.car.mapper.CarOrderMapper;
import com.ruoyi.car.service.ICarGoodsService;
import com.ruoyi.car.service.ICarOrderService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 订单管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@Service
public class CarOrderServiceImpl implements ICarOrderService {
    @Autowired
    private CarOrderMapper carOrderMapper;
    @Autowired
    private ICarGoodsService carGoodsService;

    private static String ALL_PATH_FULL = "[{\"text\":\"支付定金\"},{\"text\":\"生产中\"},{\"text\":\"车辆到店\"},{\"text\":\"车辆交付\"}]";
    private static String ALL_PATH_LOAD = "[{\"text\":\"贷款申请\"},{\"text\":\"贷款通过\"},{\"text\":\"生产中\"},{\"text\":\"车辆到店\"},{\"text\":\"车辆交付\"}]";

    /**
     * 查询订单管理
     *
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public CarOrder selectCarOrderById(Long id) {
        return carOrderMapper.selectCarOrderById(id);
    }

    /**
     * 查询订单管理列表
     *
     * @param carOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<CarOrder> selectCarOrderList(CarOrder carOrder) {
        List<CarOrder> carOrders = carOrderMapper.selectCarOrderList(carOrder);
        this.fillOrderList(carOrders, carOrder.getNeedGoodsInfo());
        return carOrders;
    }

    private void fillOrderList(List<CarOrder> orderList, Boolean needGoodsInfo) {
        if (CollectionUtils.isEmpty(orderList)) {
            return;
        }
        for (CarOrder carOrder : orderList) {
            this.formatOrderStatus(carOrder);
        }
        if (Objects.equals(needGoodsInfo, true)) {
            for (CarOrder carOrder : orderList) {
                CarGoods carGoods = carGoodsService.selectCarGoodsById(carOrder.getGoodsId());
                carOrder.setGoods(carGoods);
            }
        }
    }

    private void formatOrderStatus(CarOrder carOrder) {
        String allPath = carOrder.getAllPath();
        String currentPath = carOrder.getCurrentPath();
        if (StringUtils.isEmpty(allPath)) {
            return;
        }
        if (StringUtils.isEmpty(currentPath)) {
            return;
        }
        JSONArray array = JSON.parseArray(allPath);
        JSONObject jsonObject = array.getJSONObject(Integer.parseInt(currentPath));
        String desc = jsonObject.getString("text");
        carOrder.setCurrentPathDesc(desc + "（" + carOrder.getWay() + "）");
    }

    /**
     * 新增订单管理
     *
     * @param carOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertCarOrder(CarOrder carOrder) {
        this.beforeSave(carOrder);
        carOrder.setCreateTime(DateUtils.getNowDate());
        return carOrderMapper.insertCarOrder(carOrder);
    }

    /**
     * 修改订单管理
     *
     * @param carOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateCarOrder(CarOrder carOrder) {
        this.beforeSave(carOrder);
        carOrder.setUpdateTime(DateUtils.getNowDate());
        return carOrderMapper.updateCarOrder(carOrder);
    }

    private void beforeSave(CarOrder carOrder) {
        Long goodsId = carOrder.getGoodsId();
        if (goodsId == null) {
            throw new ServiceException("goodsId不能为空");
        }
        CarGoods carGoods = carGoodsService.selectCarGoodsById(goodsId);
        if (carGoods == null) {
            throw new ServiceException("所购车辆不存在");
        }
        carOrder.setGoodsName(carGoods.getGoodsName());
        if (StringUtils.isEmpty(carOrder.getAllPath())) {
            if (Objects.equals(carOrder.getWay(), "全款")) {
                carOrder.setAllPath(ALL_PATH_FULL);
                if (carGoods.getIsCar()) {
                    carOrder.setCurrentPath("2");
                } else {
                    carOrder.setCurrentPath("1");
                }
            } else {
                carOrder.setAllPath(ALL_PATH_LOAD);
                carOrder.setCurrentPath("0");
            }

        }
    }

    /**
     * 批量删除订单管理
     *
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteCarOrderByIds(Long[] ids) {
        return carOrderMapper.deleteCarOrderByIds(ids);
    }

    /**
     * 删除订单管理信息
     *
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteCarOrderById(Long id) {
        return carOrderMapper.deleteCarOrderById(id);
    }

    @Override
    public int updateCarOrderGoodsName(String goodsName, Long goodsId) {
        return carOrderMapper.updateCarOrderGoodsName(goodsName, goodsId);
    }

    @Override
    public int countAll() {
        return carOrderMapper.countAll();
    }

    @Override
    public List<CarTypeOrderCountVo> countByCarType() {
        return carOrderMapper.countByCarType();
    }

    @Override
    public List<NameValueVo> countByWay() {
        return carOrderMapper.countByWay();
    }
}
